<?php

namespace app\api\model;

use think\Model;


class Users extends Model
{

    // 表名
    protected $table = 'tp_users';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'first_leader_name',
        'sex_text',
        'level_text'
    ];

    public function getLevelList(){
        return [
            '0'=>'市场专员',
            '1'=>'服务经理',
            '2'=>'服务总监',
            '3'=>'总经理',
        ];
    }

    public function getFirstLeaderNameAttr($value, $data){
        $value = $value ? $value : ($data['first_leader'] ?? "");
        if(empty($value)){
            return null;
        }
        return Users::where('user_id',$value)->value('nickname');
    }

    public function getSexTextAttr($value, $data){
        $value = $value ? $value : ($data['sex'] ?? "");
        return $value==0?'保密':($value==1?'男':'女');
    }

    public function getRegTimeAttr($value, $data){
        $value = $value ? $value : ($data['reg_time'] ?? "");
        return date("Y-m-d H:i:s",$value);
    }

    public function getLevelTextAttr($value, $data){
        $value = $value ? $value : ($data['vip_level'] ?? "");
        if($data['is_vip']==0){
            return "普通会员";
        }
        return $this->getLevelList()[$value];
    }
}
